<template>
  <view class="data-card" @click.stop="onTableClick" @dblclick="onDblClick" style="background: white;"
        v-if="options && options.seriesSetting">
    <u-image class="data-card-image"
             v-if="options.seriesSetting.icon"
             :src="options.seriesSetting.icon"
             :height="options.seriesSetting.iconSize + 'px'"
             :width="options.seriesSetting.iconSize + 'px'"
             :borderRadius="options.seriesSetting.iconRadius + 'px'"
             shape="square"/>
    <view class="data-card-title" :style="{
					bold: options.seriesSetting.mainTextStyle.bold,
					italics: options.seriesSetting.mainTextStyle.italics,
					color: options.seriesSetting.mainTextStyle.color,
					fontStyle: options.seriesSetting.mainTextStyle.italics ?
						'italic' : undefined,
					fontWeight: options.seriesSetting.mainTextStyle.bold ?
						'bold' : undefined,
					fontSize: options.seriesSetting.mainTextStyle.fontSize + 'px'
				}">
      {{ getDataString("mainTextColumn", "卡片标题", dataObject) }}
    </view>
    <view class="data-card-number" :style="{
					bold: options.seriesSetting.numTextStyle.bold,
					italics: options.seriesSetting.numTextStyle.italics,
					color: options.seriesSetting.numTextStyle.color,
					fontStyle: options.seriesSetting.numTextStyle.italics ?
						'italic' : undefined,
					fontWeight: options.seriesSetting.numTextStyle.bold ?
						'bold' : undefined,
					fontSize: options.seriesSetting.numTextStyle.fontSize + 'px'
				}">
      {{ getDataString("numTextColumn", "", dataObject) }}
    </view>
    <view class="data-card-footer" v-if="isShowFootText || isShowFootNum">
      <view v-if="isShowFootText" :style="{
					bold: options.seriesSetting.footTextStyle.bold,
					italics: options.seriesSetting.footTextStyle.italics,
					color: options.seriesSetting.footTextStyle.color,
					fontStyle: options.seriesSetting.footTextStyle.italics ?
						'italic' : undefined,
					fontWeight: options.seriesSetting.footTextStyle.bold ?
						'bold' : undefined,
					fontSize: options.seriesSetting.footTextStyle.fontSize + 'px'
				}">
        {{ getDataString("footTextColumn", "", dataObject) }}
      </view>
      <view v-if="isShowFootNum" :style="{
					bold: options.seriesSetting.footNumTextStyle.bold,
					italics: options.seriesSetting.footNumTextStyle.italics,
					color: options.seriesSetting.footNumTextStyle.color,
					fontStyle: options.seriesSetting.footNumTextStyle.italics ?
						'italic' : undefined,
					fontWeight: options.seriesSetting.footNumTextStyle.bold ?
						'bold' : undefined,
					fontSize: options.seriesSetting.footNumTextStyle.fontSize + 'px',
					color: getFooterNumIcon === 'el-icon-top-right' ? '#00A241' : '#EF502F'
				}">
        <view class="iconfont" v-if="getFooterNumIcon !== ''" :class="getFooterNumIcon"/>
        {{ getShowFooterNumText }}
      </view>
    </view>
  </view>
</template>

<script>
import chartMixins from './chartMixins.js';

export default {
  name: 'dataCard',
  props: {
    // 透视表数据
    data: {
      type: [Object, Array]
    },
    // 透视表高度
    height: {
      type: String
    },
    // 透视表设置
    options: {
      type: Object
    },
    widget: {
      type: Object
    }
  },
  mixins: [chartMixins],
  components: {},
  data() {
    return {};
  },
  methods: {
    onTableClick() {
      this.$emit('click');
    },
    onDblClick() {
      this.$emit('dblclick', this.dataObject);
    },

    getTextStyle(textStyleName) {
      return {
        ...this.options.seriesSetting[textStyleName],
        fontStyle: this.options.seriesSetting[textStyleName].italics ?
            'italic' : undefined,
        fontWeight: this.options.seriesSetting[textStyleName].bold ?
            'bold' : undefined,
        fontSize: this.options.seriesSetting[textStyleName].fontSize + 'px'
      };
    }
  },
  computed: {
    isShowFootText() {
      return (
          this.options && this.options.datasetInfo &&
          this.options.datasetInfo.footTextColumn &&
          this.options.datasetInfo.footTextColumn.length > 0
      );
    },
    isShowFootNum() {
      return (
          this.options && this.options.datasetInfo &&
          this.options.datasetInfo.footNumTextColumn &&
          this.options.datasetInfo.footNumTextColumn.length > 0
      );
    },
    getFooterNumIcon() {
      if (!isNaN(this.getFooterNumText)) {
        // 如果是数字
        if (this.getFooterNumText >= 0) {
          return 'el-icon-top-right';
        } else {
          return 'el-icon-bottom-left';
        }
      } else {
        return '';
      }
    },
    getFooterNumText() {
      let numText = this.getDataString('footNumTextColumn', '', this.dataObject);
      if (!isNaN(numText)) {
        return Number(numText);
      } else {
        return numText;
      }
    },
    getShowFooterNumText() {
      if (!isNaN(this.getFooterNumText)) {
        return Math.abs(this.getFooterNumText);
      } else {
        return this.getFooterNumText;
      }
    }
  }
};
</script>

<style scoped>
.data-card {
  display: flex;
  flex-direction: column;
  position: relative;
  min-height: 220rpx;
  cursor: pointer;
}

.data-card-number {
  line-height: 2.0;
  text-align: left;
  flex-grow: 1;
  flex-shrink: 0;
  height: 80rpx;
}

.data-card-title {
  flex-grow: 0;
  flex-shrink: 0;
}

.data-card-footer {
  flex-grow: 0;
  flex-shrink: 0;
  border-top: 1px solid #E8E8E8;
  padding-top: 20rpx;
  display: flex;
  justify-content: space-between;
}

.data-card-image {
  position: absolute;
  top: 0px;
  right: 0px;
}
</style>
